﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
using Newtonsoft.Json.Serialization;
using System.Web.Http.Cors;
using Microsoft.Practices.Unity.WebApi;
using Microsoft.Practices.Unity;
using System.Web.Http.Filters;
using FollowMe.API.Controllers;
using FollowMe.Services;
using System.Web.Http.Controllers;
using Newtonsoft.Json;
using System.Net.Http.Formatting;
using FollowMe.API.App_Start;

namespace FollowMe.API
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {

            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));


            //Enable CORS to allow the API to work from a client.
            var cors = new EnableCorsAttribute("*", "*", "GET,POST,OPTIONS,PUT,DELETE");
            config.EnableCors();

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            //Set json as default for http/browsers
            config.Formatters.Add(new JsonMediaFormatter());

            //ignore camel case in jsons files
            var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            json.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            JsonMediaTypeFormatter jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
            JsonSerializerSettings jSettings = new Newtonsoft.Json.JsonSerializerSettings()
            {
                Formatting = Formatting.Indented,
                DateTimeZoneHandling = DateTimeZoneHandling.Utc
            };
            jSettings.Converters.Add(new DateTimeConvertor());
            jsonFormatter.SerializerSettings = jSettings;

            // Remove the XML formatter
            //config.Formatters.Remove(config.Formatters.XmlFormatter);
        }
    }
}